package com.system.permissiondemo.interceptor;

import com.system.permissiondemo.filter.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private HttpInterceptor httpInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(httpInterceptor).addPathPatterns("/**");
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        // 新建过滤器注册类
        FilterRegistrationBean registration = new FilterRegistrationBean();
        // 添加自定义 过滤器
        registration.setFilter(new LoginFilter());
        List<String> urlList = new ArrayList<>();
        urlList.add("/sys/*");
        // 设置过滤器的URL模式
        registration.setUrlPatterns(urlList);
        // 设置过滤器名称
        registration.setName("LoginFilter");
        // 设置过滤器顺序
        registration.setOrder(1);
        return registration;
    }
}
